<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/mytag.jspf"%>

<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<title>user</title>
<script type="text/javascript">
	//初始化下拉框
	$.ajax({
		url: "management/shop/listshopGrade",
		type:"POST",
		success:function(data){
			$(data).each(function(n){
	            $("<option/>").html(this.name).val(this.shopGradeId)
	            .appendTo("#shopGradeId");
	        });
		}
	});
	var treeObj = null,curNode = null;
	treeObj = $.fn.zTree.init($("#treeDemo"), {
		async: {enable:true,url:"management/shop/listshop"},
		data: {simpleData: {enable:true,idKey:"shopId",pIdKey:"pshopId"}},
		view: {selectedMulti: false},
		callback: {
			onClick: function(e, treeId, node){
				
				if(node==curNode){
					tree.cancelSelectedNode(node);
					alert(node.name+node.id);
					curNode = null;
				} else {
					$.ajax({
						url: "management/shop/shopInfo/"+node.shopId,
						type:"POST",
						success:function(data){
				    		backFile(data);
						}
					});
					curNode = node;
				}
			},
			onAsyncSuccess:function(){
				treeObj.expandAll(true);//所有节点展开
				
				var nodes=treeObj.getNodes();//获取所有节点
				//nodes[0].shopId 获取第一个节点的id值
				var node = treeObj.getNodeByParam("shopId", nodes[0].shopId);//获取第一个节点的控件id
				$("#"+node.tId+"_a").click();//模拟点击事件
			}

		}
	});

	function backFile(result){
		$("input[name='name']").val(result.name);
		//$("input[name='shopGradeName']").val(result.shopGradeName);
	//	alert(result.shopGradeId);
		$("#shopGradeId").val(result.shopGradeId);
		$("input[name='phone']").val(result.phone);
		$("input[name='pshopName']").val(result.pshopName);
		$("input[name='shopId']").val(result.shopId);
		$("input[name='shopGradeId']").val(result.shopGradeId);
		$("input[name='pshopId']").val(result.pshopId);
		$("input[name='compName']").val(result.compName);
		$("input[name='compId']").val(result.compId);
		$("input[name='addr']").val(result.addr);
		$("input[name='photo']").val(result.photo);
		if(result.photo!==null){
			$("#imgId").attr("src",result.photo);
		}else{
			$("#imgId").attr("src","file/a.png");
		}
		$("input[name='introduce']").val(result.introduce);
	///	$("#contents").val(result.contents);
	}
	function deleteNode(){
		layer.confirm('确定删除？', function(index) {
			var id = $("input[name='shopId']").val();
			var preNode = treeObj.getNodeByParam("shopId", id);//获取当前节点
			//判断该节点是否有子节点，有就不能删除
			if(preNode.isParent){
				layer.msg("有下级门店不能删除");
				return ;
			}
			var url = "management/shop/delete/"+id;
			$.ajax({
				type : 'POST',
				url : url,
				dataType : 'json',
				success : function(data) {
					layer.msg(data.msg);
					if (data.success) {
						var preNode = treeObj.getNodeByParam("shopId", id);//获取当前节点
						treeObj.removeNode(preNode);
						treeObj.refresh(); //重新载入 
						var nodes=treeObj.getNodes();//获取所有节点
						var node = treeObj.getNodeByParam("shopId", nodes[0].shopId);//获取第一个节点的控件id
						$("#"+node.tId+"_a").click();//模拟点击事件
					}
				}
			});
		});
	}
	function addShop(){
		var id = $("input[name='shopId']").val();
		var url="${ctx}/management/shop/add/"+id;
		//window.open(url)
		window.location.href = url;
	}
	function upImage(){
		$("#imgForm").ajaxSubmit({
			dataType : "json",
			success : function(data) {
				if (data.success == true) {
					var url = data.msg;
					$("#imgId").attr("src",url);
					$("#photo").val(url);
				} else {
					alert(data.msg);
				}
			}
		});
	}
	$(function(){
		$("#form").validate({
			rules : {
				"shopGradeId" : {
					required : true
				},
				"name" : {
					required : true
				},
				"phone" : {
					isContact : true
				}
			},
			messages : {
				"shopGradeId" : {
					required : "请选择门店等级"
					
				},
				"name" : {
					required : "请输入门店名称"
				},
				"phone" : {
					isContact : "请输入正确的联系电话"
				}
			}
		});
		$("#submitBtn").on("click",function(){
			$("#form").submit();
		});
	});
</script>
</head>

<body>
<div class="am-g">
		<div class="am-u-md-2">
			<div class="am-btn-toolbar">
				<div class="am-btn-group am-btn-group-sm">
					<ul id="treeDemo" class="ztree"></ul>
				</div>
			</div>
		</div>
		<div class="am-u-md-10">
				<div class="am-tabs am-margin">
					<ul class="am-tabs-nav am-nav am-nav-tabs">
						<li class="am-active"><a href="javascript:void(0)">门店信息</a></li>
					</ul>
					<div class="am-tabs-bd">
						<div class="am-tab-panel am-active">
							<div class="am-g">
								<div class="am-u-sm-12">
									<form id="imgForm"  class="am-form am-form-horizontal am-block" action="management/file" method="post" enctype="multipart/form-data">	
										<label class="am-u-sm-2 am-form-label">门店照片</label>
										<div class="am-u-sm-4 am-u-end">
											<img id="imgId" src="file/a.png" class="am-form-field am-input-sm">
											<div class="am-form-group am-form-file">
											  <button type="button" class="am-btn am-btn-default am-btn-sm">
											    <i class="am-icon-cloud-upload"></i> 选择要上传的图片</button>
											  <input name="imageFile" id="imageFile" onchange="upImage();" type="file" multiple>
											</div>
										</div>
									</form>
								</div>
								<div class="am-u-sm-12">
									<form class="am-form am-form-horizontal" id="form" name="form"  method="post" action="management/shop/update">
									<input type="hidden" name="photo" id="photo"  class="am-form-field am-input-sm">
									<input type="hidden" name="compId" id="compId"  class="am-form-field am-input-sm"><!-- shopId -->
									<input type="hidden" name="shopId" id="shopId"  class="am-form-field am-input-sm"><!-- shopId -->
									<input type="hidden" name="pshopId" id="pshopId"  class="am-form-field am-input-sm"><!-- pshopId -->
								<!-- <div class="am-form-group">
										<label class="am-u-sm-2 am-form-label">所属企业</label>
										<div class="am-u-sm-4 am-u-end">
											<input type="text" id="compName" name="compName" class="am-form-field am-input-sm">
										</div>
									</div>
								-->
									<div class="am-form-group">
										<label class="am-u-sm-2 am-form-label">门店名称</label>
										<div class="am-u-sm-4 am-u-end">
											<input type="text" id="name" name="name" maxlength="50" class="am-form-field am-input-sm">
										</div>
									</div>
								
									<div class="am-form-group">
										<label class="am-u-sm-2 am-form-label">门店等级</label>
										<div class="am-u-sm-4 am-u-end">
											<select id="shopGradeId" name="shopGradeId" >
												<option value="">请选择门店等级</option>
											</select>
										</div>
									</div>
									<div class="am-form-group">
										<label class="am-u-sm-2 am-form-label">联系电话</label>
										<div class="am-u-sm-4 am-u-end">
											<input type="text" id="phone" name="phone" maxlength="20" class="am-form-field am-input-sm">
										</div>
									</div>
									<div class="am-form-group">
										<label class="am-u-sm-2 am-form-label">上级门店</label>
										<div class="am-u-sm-4 am-u-end">
											<input type="text" id="pshopName" name="pshopName" class="am-form-field am-input-sm" readonly="readonly">
										</div>
									</div>
									<div class="am-form-group">
										<label class="am-u-sm-2 am-form-label">门店地址</label>
										<div class="am-u-sm-4 am-u-end">
											<input type="text" id="addr" name="addr" maxlength="50" class="am-form-field am-input-sm">
										</div>
									</div>
									<div class="am-form-group">
										<label class="am-u-sm-2 am-form-label">门店介绍</label>
										<div class="am-u-sm-4 am-u-end">
											<input type="text" id="introduce" name="introduce" maxlength="150"  class="am-form-field am-input-sm">
										</div>
									</div>
								</form>
								</div>
							
							</div>
						</div>
					</div>
				</div>
				<div class="am-margin">
					<button class="am-btn am-btn-primary am-btn-xs" type="button" onclick="addShop();"><span class="am-icon-check"></span>新增下级门店</button>
					<button class="am-btn am-btn-primary am-btn-xs" type="button" id="submitBtn"><span class="am-icon-check"></span>修改门店</button>
					<button class="am-btn am-btn-primary am-btn-xs" type="button" onclick="deleteNode();"><span class="am-icon-check"></span>删除</button>
				</div>
		</div>
</div>
</body>
</html>
